<?php
require_once('Spreadsheet/Excel/Writer.php');

class SimpleTableExporter{
	
	/**
	 * 导出时的文件名称
	 *
	 * @var string
	 */
	private $filename;
	
	/**
	 * Excel 表格
	 *
	 * @var Spreadsheet_Excel_Writer
	 */
	private $workbook;
	
	function SimpleTableExporter($filename = null){
		if($filename == null){
			$this->filename = date('Ymd_His').'.xls';
		}else{
			$this->filename = $filename;
		}
		$this->workbook = new Spreadsheet_Excel_Writer();
	}
	/**
	 * 向workbook中插入总表数据，
	 * $a为数组，其中的数据是有顺序的，顺序为：
	 * 见下面$ths 变量的元素顺序
	 * 
	 *
	 * @param array $array
	 */
	function insertMainData($a){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk','总表'));
		$format = &$this->workbook->addFormat(array('color' => 'green'));
		$ths = array('产品质量证明书号','产品质量证明书编号','批次','生产厂家','船级','材质','厚','宽','长','数量','单重','重量',
				'订单号','订单子项号','受订单价','物料号','发货批次','批号','发车证书号','购单号','发货时间','合同月份','车号',
				'销售产家','备注','上传文件名','上传时间');
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 0;
		foreach($a as $col){
			$y = 1;
			if(is_array($col))
			foreach($col as $td){
				$td = iconv('utf-8', 'gbk', $td);
				$worksheet->write($y, $x, $td, $format);
				$y++;
			}
			$x++;
		}
	}
	
	function insertMainTableData($a){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk','总表'));
		$format = &$this->workbook->addFormat(array('color' => 'green'));
		$ths = array('材料代码','产品质量证明书号','产品质量证明书编号','发车证书号','批次',
					 '生产厂家','船级','材质','厚','宽','长','单重',
					 '订单号','订单子项号','受订单价','物料号',
					 '发货批次','批号','购单号', '发货时间',
					 '合同月份','车号','销售产家','备注','上传文件名','上传时间',
					 '订货清单数量','材料代码数量','订货清单总数量','质量证书分配数量',
					 '批号数量','质量证书号数量','质量证书号总数量');
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 0;
		foreach($a as $col){
			$y = 1;
			if(is_array($col))
			foreach($col as $td){
				$td = iconv('utf-8', 'gbk', $td);
				$worksheet->write($y, $x, $td, $format);
				$y++;
			}
			$x++;
		}
	}

	function insertMain3Data($a){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk','总表3'));
		$format = &$this->workbook->addFormat(array('color' => 'green'));
		$ths = array('材料代码','产品质量证明书号','批次','生产厂家','船级','材质','厚','宽','长','数量','单重',
				'订单号','订单子项号','受订单价','购单号','备注','上传文件名','上传时间');
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 0;
		foreach($a as $col){
			$y = 1;
			if(is_array($col))
			foreach($col as $td){
				$td = iconv('utf-8', 'gbk', $td);
				$worksheet->write($y, $x, $td, $format);
				$y++;
			}
			$x++;
		}
	}
	
	/**
	 * 向workbook中插入发车数据，
	 * $a为数组，其中的数据是有顺序的，顺序为：
	 * 见下面$ths 变量的元素顺序
	 *
	 * @param array $a
	 */
	function insertRukuData($a){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk', '入库'));
		$format = &$this->workbook->addFormat(array('color' => 'blue'));
		$ths = array('入库时间','产品质量证明书号','产品质量证明书编号','发货批次','备注一','备注二','上传文件名','上传时间');
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 0;
		foreach($a as $col){
			$y = 1;
			if(is_array($col))
			foreach($col as $td){
				$td = iconv('utf-8', 'gbk', $td);
				$worksheet->write($y, $x, $td, $format);
				$y++;
			}
			$x++;
		}
	}
	
	/**
	 * 向workbook中插入发船数据，
	 * $a为数组，其中的数据是有顺序的，顺序为：
	 * 见下面$ths 变量的元素顺序
	 *
	 * @param unknown_type $a
	 */
	function insertChukuData($a){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk', '出库'));
		$format = &$this->workbook->addFormat(array('color' => 'red'));
		$ths = array('出库时间','产品质量证明书号','产品质量证明书编号','经手人','发货批次','备注一','备注二','上传文件名','上传时间');
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 0;
		foreach($a as $col){
			$y = 1;
			if(is_array($col))
				foreach($col as $td){
					$td = iconv('utf-8', 'gbk', $td);
					$worksheet->write($y, $x, $td, $format);
					$y++;
				}
			$x++;
		}
	}
	
	function insertCustomData($a, $ths, $sheetname, $color = 'black'){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk', $sheetname));
		$format = &$this->workbook->addFormat(array('color' => $color));
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 0;
		foreach($a as $col){
			$y = 1;
			if(is_array($col))
				foreach($col as $td){
					$td = iconv('utf-8', 'gbk', $td);
					$worksheet->write($y, $x, $td, $format);
					$y++;
				}
			$x++;
		}
	}
	
	function insertDownloadData($a, $ths, $sheetname, $color = 'black'){
		// Creating a worksheet
		$worksheet =& $this->workbook->addWorksheet(iconv('utf-8', 'gbk', $sheetname));
		$format = &$this->workbook->addFormat(array('color' => $color));
		$x = 0;
		foreach($ths as $th){
			$th = iconv('utf-8', 'gbk', $th);
			$worksheet->write(0, $x, $th, $format);
			$x++;
		}
		$x = 1;
		foreach($a as $col){
			$y = 0;
			if(is_array($col))
				foreach($col as $td){
					$td = iconv('utf-8', 'gbk', $td);
					$worksheet->write($x, $y, $td, $format);
					$y++;
				}
			$x++;
		}
	}
	
	/**
	 * 导出表格
	 *
	 */
	function export(){
		// sending HTTP headers
		$this->workbook->send($this->filename);
		$this->workbook->close();
	}
	
	function getWorkbook(){
		return $this->workbook;
	}
}

?>